setwd("F:/cuffdiff/superdiff五种通路注释")

read.kegg <- function(input, sheet){
  if(!require(openxlsx)){
    install.aocakges("openxlsx")
    if(!require(openxlsx)){
      stop("openxlsx install failed")
    }
  }
  input <- read.xlsx(as.character(input), sheet = sheet)
  
  # 用来计算RichFactor，用的是其中的BgRatio
  caculate.richfactor <- function(input){
    value <- unlist(strsplit(input[4], "/"))
    return(as.numeric(as.character(value[1]))/as.numeric(as.character(value[2])))
  }
  
  input$RichFactor <- apply(input, 1, caculate.richfactor)

  return(input)
}

# 根据clusterProfile包的结果，画kegg的图
draw.clusterprofile <- function(input)
{
  if(!require(ggplot2)){
    install.packages("ggplot2")
    if(!require(ggplot2)){
      stop("ggplot2 install failed")
    }
  }
  
  # window specific
  # 改变字体为Times New Roman
  windowsFonts(NRM = windowsFont("Times New Roman"))
  old_theme <- theme_update(axis.text = element_text(family = "NRM", size = 18, color = "black"),
                            legend.key.size = unit(1.2, "cm"),
                            axis.title = element_text(family = "NRM", size = 18),
                            legend.text = element_text(family = "NRM", size = 18))
  
  p <- ggplot(input,
              aes(x = RichFactor,
                  y = reorder(Description, qvalue, sort),
                  colour = qvalue,
                  size = Count)
  ) + geom_point() + scale_colour_gradientn(colours=rainbow(4)) + ylab("Description") 
  return(p)
}

kegg <- read.kegg("superdiffkegg.xlsx", 3)

tiff("hsa-miR-4763-3p_kegg.tiff", units = "in", width = 12, height = 10, compression = "lzw", res = 600)
draw.clusterprofile(kegg) + theme(text = element_text(size=20))
dev.off()

kegg <- read.kegg("superdiffkegg.xlsx", 5)

tiff("hsa-miR-6825-5p_kegg.tiff", units = "in", width = 12, height = 10, compression = "lzw", res = 600)
draw.clusterprofile(kegg) + theme(text = element_text(size=20))
dev.off()

##### GO
read.go <- function(input, sheet){
  if(!require(openxlsx)){
    install.aocakges("openxlsx")
    if(!require(openxlsx)){
      stop("openxlsx install failed")
    }
  }
  input <- read.xlsx(as.character(input), sheet = sheet)
  
  # 用来计算RichFactor，用的是其中的BgRatio
  caculate.richfactor <- function(input){
    value <- unlist(strsplit(input[4], "/"))
    return(as.numeric(as.character(value[1]))/as.numeric(as.character(value[2])))
  }
  input$RichFactor <- apply(input, 1, caculate.richfactor)
  
  cc <- subset(input, type == "cellular component")
  bp <- subset(input, type == "biological process")
  mf <- subset(input, type == "molecular funtion")
  
  real.extract <- function(input){
    big <- max(input$RichFactor)
    small <- min(input$RichFactor)
    
    twen <- big - ((big - small) * 0.2)
    
    return(input[input$RichFactor > twen,])
  }
  
  cc <- real.extract(cc)
  bp <- real.extract(bp)
  mf <- real.extract(mf)
  
  all <- rbind(cc, bp)
  input <- rbind(all, mf)
  
  return(input)
}


draw.go <- function(input){
  if(!require(ggplot2)){
    install.packages("ggplot2")
    if(!require(ggplot2)){
      stop("ggplot2 install failed")
    }
  }
  if(!is.data.frame(input)){
    input <- as.data.frame(input)
  }
  # window specific
  # 改变字体为Times New Roman
  windowsFonts(NRM = windowsFont("Times New Roman"))
  old_theme <- theme_update(axis.text = element_text(family = "NRM", size = 18, color = "black"),
                            legend.key.size = unit(1.2, "cm"),
                            axis.title = element_text(family = "NRM", size = 18),
                            legend.text = element_text(family = "NRM", size = 18))
  
  
  p <- ggplot(input, aes(x = reorder(Description, Count, sort)          # 对画图的柱状，进行一个排序
                         , y= Count, fill = type))+
    geom_bar(stat = "identity") + facet_grid(~type, scales = "free_x") + 
    theme(axis.text.x = element_text(angle = 80, hjust = 1),
          strip.text = element_text(size = 20)) + xlab("GO Term") + 
    guides(fill = FALSE)
  return(p)
}

go <- read.go("superdiffgo.xlsx", 3)
tiff("hsa-miR-4763-3p_go.tiff", units = "in", width = 12, height = 10, compression = "lzw", res = 600)
draw.go(go)
dev.off()


go <- read.go("superdiffgo.xlsx", 5)
tiff("hsa-miR-6825-5p_go.tiff", units = "in", width = 12, height = 10, compression = "lzw", res = 600)
draw.go(go)
dev.off()